package com.gome.han.bigdata.hdfs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
/**
* @author hanpeng
* @version 创建时间：2020年11月18日 下午5:14:15
* @ClassName 类名称
* @Description 类描述
*/
public class HDFSClient {
public static void main(String[] args) throws IOException, Exception, URISyntaxException {
		
		Configuration conf = new Configuration();
//		conf.set("fs.defaultFS", "hdfs://hadoop102:9000");
		
		// 1 获取hdfs客户端对象
//		FileSystem fs = FileSystem.get(conf );
        //	URI 对应core-site.xml 	
		/*<property>
			<name>fs.default.name</name>
			<value>hdfs://elk:8020</value>
	    </property>*/
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.43.205:8020"), conf, "root");
		
		
		// 2 在hdfs上创建路径
		fs.mkdirs(new Path("/0529/dashen/banzhang222"));
		
		// 3 关闭资源
		fs.close();
		
		System.out.println("over");
	}

	@Test
	public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {

		// 1 获取fs对象
		Configuration conf = new Configuration();
		conf.set("dfs.replication", "2");
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.43.205:8020"), conf, "root");

		// 2 执行上传API
		fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/xiaohua.txt"));

		// 3 关闭资源
		fs.close();
	}
	// 把本地e盘上的banhua.txt文件上传到HDFS根目录
	@Test
	public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {

		// 1 获取对象
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "atguigu");

		// 2 获取输入流
		FileInputStream fis = new FileInputStream(new File("E:/input/hello.txt"));

		// 3 获取输出流
		FSDataOutputStream fos = fs.create(new Path("/banzhang.txt"));

		// 4 流的对拷
		IOUtils.copyBytes(fis, fos, conf);

		// 5 关闭资源
		IOUtils.closeStream(fos);
		IOUtils.closeStream(fis);
		fs.close();
	}
	// 从HDFS上下载banhua.txt文件到本地e盘上
	@Test
	public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException {

		// 1 获取对象
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "atguigu");

		// 2 获取输入流
		FSDataInputStream fis = fs.open(new Path("/banhua.txt"));

		// 3 获取输出流
		FileOutputStream fos = new FileOutputStream(new File("e:/banhua.txt"));

		// 4 流的对拷
		IOUtils.copyBytes(fis, fos, conf);

		// 5 关闭资源
		IOUtils.closeStream(fos);
		IOUtils.closeStream(fis);
		fs.close();
	}
}
